package com.rst.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.rst.model.ExamScore;

@Repository
@Transactional
public class ExamScoresDAO {

	@Autowired
	private SessionFactory sessionFactory;

	
	public void saveExamScore(ExamScore examScore) {
		sessionFactory.getCurrentSession().save(examScore);		
	}

	public ExamScore getExamScoreById(Long examScoreId) {
	    Session session = sessionFactory.getCurrentSession();
	    ExamScore examScore = (ExamScore)session.get(ExamScore.class, examScoreId);
		return examScore;
	}

	@SuppressWarnings("unchecked")
	public List<ExamScore> getExamScoreList(Long examId) {
		Session session = sessionFactory.getCurrentSession();
	    String str = "FROM ExamScore eg WHERE eg.examId = :examId  AND eg.status = :status  ";
	    	
	    Query query = session.createQuery(str);
	    query.setParameter("status",  ExamScore.ACTIVE);
	    query.setParameter("examId",  examId);
	    
	    List<ExamScore> examScoreList = query.list(); 
	    for (ExamScore examScore : examScoreList) {
	    	 Hibernate.initialize(examScore.getStudent());
	    	 Hibernate.initialize(examScore.getStudent().getUserDetails());
		}
	    
		return query.list();
	}

	public void updateExamScore(ExamScore examScore) {
		sessionFactory.getCurrentSession().update(examScore);
		
	}
}




